import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Table} from 'antd';
import {TABLE} from '../constants';
import {conventEnumValueToString} from '../utils';
import {baseDataStatus} from '../Enum';
import {hasPermissions} from './utils';
import {PERMISSION} from './constants';
import WrappedPopconfirm from '../common/WrappedPopconfirm';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import {abandonData} from './api';
import styles from './style.css';
import routes from './routes';
const {Description} = DescriptionList;

export class DetailPanel extends React.PureComponent {
    state = {
        loading: false,
    };
    componentDidMount() {
        this.props.init();
    }
    handleUpdateClick = () => {
        this.props.history.push(routes.update.format(this.props.id));
    }
    abandonConfirm = () => {
        this.setState({loading: true});
        const rowVersion = this.props.detail.rowVersion;
        abandonData(this.props.detail.id, rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    render() {
        const detailData = this.props.detail || {};
        const {options, details} = detailData;
        const {formatMessage} = this.props.intl;
        const operations = [];
        if(hasPermissions(this.props.permissions, PERMISSION.update) && hasPermissions(options, PERMISSION.update))
            operations.push(<Button key="update" onClick={this.handleUpdateClick} size="small">{
                formatMessage({
                    id: 'detailPanel.btn.edit',
                    defaultMessage: '编辑'
                })
            }</Button>);
        if(hasPermissions(this.props.permissions, PERMISSION.abandon) && hasPermissions(options, PERMISSION.abandon))
            operations.push(
                <WrappedPopconfirm
                    id="abandon"
                    key="abandon"
                    title={formatMessage({
                        id: 'detailPanel.popconfirm.message',
                        defaultMessage: '是否继续?'
                    })}
                    onConfirm={this.abandonConfirm}>
                    <Button key="abandon" size="small">{
                        formatMessage({
                            id: 'detailPanel.btn.abandon',
                            defaultMessage: '作废'
                        })
                    }</Button>
                </WrappedPopconfirm>);
        
        const columns = [{
            title: formatMessage({
                id: 'detailPanel.column.contact',
                defaultMessage: '货代联系人'
            }),
            dataIndex: 'contact',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.contactNumber',
                defaultMessage: '货代电话'
            }),
            dataIndex: 'contactNumber',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.fax',
                defaultMessage: '货代传真'
            }),
            dataIndex: 'fax',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.email',
                defaultMessage: '货代邮件'
            }),
            dataIndex: 'email',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.remark',
                defaultMessage: '备注'
            }),
            dataIndex: 'remark',
        }];
        return (
            <div className="detail">
                <Card loading={this.state.loading}>
                    <DescriptionList size="large" title={<Row><Col span={12}>{
                        formatMessage({
                            id: 'detailPanel.card.title',
                            defaultMessage: '货代信息'
                        })
                    }</Col>
                    <Col className={`col-align-right ${styles.detail}`} span={12}>{operations}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.brandCode',
                            defaultMessage: '品牌编号',
                        })}>{this.props.detail.brandCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.brandName',
                            defaultMessage: '品牌名称',
                        })}>{this.props.detail.brandName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.code',
                            defaultMessage: '货代编码',
                        })}>{this.props.detail.code}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.name',
                            defaultMessage: '货代名称',
                        })}>{this.props.detail.name}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.shortName',
                            defaultMessage: '货代简称',
                        })}>{this.props.detail.shortName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.status',
                            defaultMessage: '状态'
                        })}>{conventEnumValueToString(baseDataStatus, this.props.detail.status)}</Description>
                    </DescriptionList>
                </Card>
                <Card>
                    <DescriptionList title={formatMessage({
                        id: 'detailPanel.table.details',
                        defaultMessage: '货代信息清单'
                    })}>
                        <Table
                            className="white-space-nowrap"
                            columns={columns}
                            rowKey="id"
                            pagination={false}
                            dataSource={details}
                            {...TABLE} />
                    </DescriptionList>
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.state.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPanel.btn.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    detail: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {getDetail, tableSearch} from './actions';
import {injectIntl} from './intl';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);
const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    permissions: getPermissions(state),
    detail: getDetailData(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    onCancel: () => {
        props.history.push(routes.query.url());
    },
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
